/*
 * GET home page.
 */
var db = require("../dao/dbMongo");
var categoryDao = require("../dao/categoryDao");
var noteDao = require("../dao/noteDao");

db.connect();

exports.index = function(req, res) {
	var categoryid = req.query.categoryid;
	console.log('[routes.index], request category id = ' + categoryid);

	if (categoryid)
	{
		categoryDao.findById(categoryid, function(err, item){
			if (!err) {
				console.log('[routes.index], query current category ,result is ' + item.title);
				req.session.category = item; // 会话缓存当前Category
			}		
		});
	}

	categoryDao.all(function(err, items) {
		if (err) {
			res.send(err);
			console.log(err);
		} else {
			console.log('[routes.index], query categories ,result is');
			console.log(items);
			noteDao.all(function(err, noteItems){
				if (err) {
					res.send(err);
					console.log(err);
				} else {
					console.log(noteItems);
					res.render('index', {
						title : '主页',
						"categories" : items,
						"noteList" : noteItems
					});
				}
			});
			
		}
	});	
}